<?php
    //-- Dispatch Script, provides various analytical functions before sending the user to the specified url parameter. --//
    set_include_path("/fs1/WWW-library-ext");
    include_once "includes/functions.php";
    // Get Student ID based on OUCU (this section to be removed for public release //
    $sams_user = mysql_real_escape_string($_SERVER['HTTP_SAMS_USER']);
    $user_id = mysql_result(mysql_query("SELECT `id` FROM `users` WHERE `oucu`='{$sams_user}' LIMIT 1"),0);
    
    //-- Processing a search result?
    if ($_GET['type']=="search") {
        $safe_keys = array("search_id","url");
        foreach ($safe_keys as $val) { ${$val} = mysql_real_escape_string($_GET[$val]); }
        $url = str_replace("&amp;","&",$url);
        if (!is_numeric($_GET['search_id'])) { header("Location: $url"); exit(); }
        $resource_id = @mysql_result(@mysql_query("SELECT `id` FROM `resources` WHERE (`ISSN`='{$issn}' AND `volume`=$vol AND `issue`=$issue) LIMIT 1"),0);
        if ((!is_numeric($resource_id)) && (!empty($name)) && ((!empty($issn))||(!empty($doi)))) {
			# Resource doesn't exist, create...
			$new_resource_q = "INSERT INTO resources ("
							. "name,url"
							. (!empty($issn)?",ISSN":"") . (!empty($vol)?",volume":"") . (!empty($issue)?",issue":"") . (!empty($doi)?",DOI":"")
							. ") VALUES('{$name}','{$url}'"
							. (!empty($issn)?",'{$issn}'":"")	. (!empty($vol)?",'{$vol}'":"")	. (!empty($issue)?",'{$issue}'":"")	. (!empty($doi)?",'{$doi}'":"")
							. ")";
			@mysql_query($new_resource_q);
			# Set $resource_id to id of newly created resource
			$resource_id = @mysql_result(@mysql_query("SELECT `id` FROM `resources` ORDER BY `id` DESC LIMIT 1"),0);
		}
        //-- Get user's last viewed resource --//
        $last_viewed_id = @mysql_result(@mysql_query("SELECT `resource_id` FROM `resource_views` WHERE `user_id`={$user_id} ORDER BY `sequence` DESC LIMIT 1"),0);
		//-- Create a resource views entry (don't input course id)
		$resource_views_q = "INSERT INTO resource_views (resource_id,user_id,search_id".(is_numeric($last_viewed_id)?",last_viewed_id":"").") VALUES ({$resource_id},{$user_id},{$search_id}".(is_numeric($last_viewed_id)?",{$last_viewed_id}":"").")";
        @mysql_query($resource_views_q);//echo $resource_id."/".$last_viewed_id;die();
        //-- Create a resource_relationships entry, or increment as applicable --//
        if (is_numeric($last_viewed_id)) {
            $res_rel_q = @mysql_query("SELECT `origin_id` FROM `resource_relationships` WHERE `origin_id`={$last_viewed_id} AND destination_id={$resource_id} LIMIT 1");
            if (mysql_num_rows($res_rel_q)>0) {
                @mysql_query("UPDATE `resource_relationships` SET value=value+1 WHERE origin_id={$last_viewed_id} AND destination_id={$resource_id}") or die(mysql_error());
            } else {
                @mysql_query("INSERT INTO `resource_relationships` (origin_id,destination_id,value) VALUES ({$last_viewed_id},{$resource_id},1)") or die(mysql_error());
            }
        }        
        # Update search relationships
        if ((is_numeric($resource_id)) && (is_numeric($search_id))) {
            $search_rel_q = @mysql_query("SELECT value FROM resource_search_relationships WHERE search_id={$search_id} AND resource_id={$resource_id} LIMIT 1");
            if (@mysql_num_rows($search_rel_q)>0) {
                @mysql_query("UPDATE resource_search_relationships SET value=value+1 WHERE search_id={$search_id} AND resource_id={$resource_id} LIMIT 1");
            } else {
                @mysql_query("INSERT INTO resource_search_relationships (search_id,resource_id,value) VALUES ({$search_id},{$resource_id},1)");
            }
        }
		header("Location: ".str_replace("&amp;","&",$_GET['url']));
    }
?>
        